package com.yonyougov.devops.cruxcisupport.version;

import com.yonyougov.devops.cruxcisupport.app.AppDao;
import com.yonyougov.devops.cruxcisupport.harbor.artifact.HarborArtifactController;
import com.yonyougov.devops.cruxcisupport.harbor.artifact.HarborArtifactDao;
import com.yonyougov.devops.cruxcisupport.harbor.artifact.HarborArtifactServiceImpl;
import com.yonyougov.devops.cruxcisupport.version.vo.VersionNameVo;
import com.yonyougov.devops.cruxcisupport.version.vo.VersionPageVo;
import com.yonyougov.devops.cruxcisupport.version.vo.VersionVo;
import com.yonyougov.devops.cruxcisupport.versioninfo.VersionInfoDao;
import com.yonyougov.devops.cruxcisupport.versioninfo.VersionInfoEntity;
import com.yonyougov.devops.fwc.core.dao.BaseDao;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * t_version服务实现
 */
@Service
public class VersionServiceImpl extends BaseServiceImpl<VersionEntity, BigInteger> implements VersionService {
    private final VersionDao versionDao;
    private final VersionInfoDao versionInfoDao;
    @Autowired
    private final AppDao appDao;
    public VersionServiceImpl(VersionDao versionDao, VersionInfoDao versionInfoDao, AppDao appDao) {
        this.versionDao = versionDao;
        this.versionInfoDao = versionInfoDao;
        this.appDao = appDao;
    }

    @Override
    public BaseDao<VersionEntity, BigInteger> getDao() {
        return versionDao;
    }

    //<!--用户自定义代码开始-->

    /**
     * 查询版本
     * 条件: 根据应用id
     *
     * @param versionEntity [com.yonyougov.devops.cruxcisupport.version.VersionEntity]
     * @return [java.util.List]
     */
    @Override
    public List<VersionNameVo> findVersionByAppId(VersionEntity versionEntity) {
        return versionDao.findAllNames(versionEntity);
    }

    /**
     * 级联删除
     * 删除版本以及版本描述
     *
     * @param versionEntity
     */
    @Override
    public void deleteCascadeByPrimaryKey(VersionEntity versionEntity) {
        List<VersionEntity> versionEntityList = versionDao.findAllBy(versionEntity);
        versionEntityList.forEach(version -> {
            VersionInfoEntity versionInfoEntity = new VersionInfoEntity();
            versionInfoEntity.setVersionId(version.getId());
            versionInfoDao.delete(versionInfoEntity);
        });
        versionDao.delete(versionEntity);
    }

    /**
     * 版本是否存在
     *
     * @param versionId 版本主键
     * @return boolean
     */
    @Override
    public boolean exist(BigInteger versionId) {
        VersionEntity versionEntity = findBy(VersionEntity.builder().id(versionId).build());
        return null != versionEntity ? true : false;
    }

    /**
     * 查询版本
     * 条件: 应用id
     *
     * @param versionEntity [com.yonyougov.devops.cruxcisupport.version.VersionEntity]
     * @return com.yonyougov.devops.cruxcisupport.version.dto.QueryVersionResponseDto
     */
    @Override
    public VersionPageVo findPageByAppId(VersionEntity versionEntity) {
        Pagination<VersionEntity> pagination = likeAllBy(versionEntity);
        List<VersionVo> versionVoList = new ArrayList<>();
        pagination.getDatas().forEach(version -> {
            VersionVo versionVo = new VersionVo();
            BeanUtils.copyProperties(version, versionVo);
            versionVoList.add(versionVo);
        });
        return new VersionPageVo(pagination.getTotalCount(), pagination.getPageCount(),
                pagination.getPageSize(), pagination.getPageNum(), versionVoList);
    }

    /**
     * 查询版本
     * 条件: 根据应用id以及状态status查询
     *
     * @param versionEntity [com.yonyougov.devops.cruxcisupport.version.VersionEntity]
     * @return [java.util.List]
     */
    @Override
    public List<VersionNameVo> findVersionByAppIdAndStatus(VersionEntity versionEntity) {
        return versionDao.findAllNames(versionEntity);
    }

    @Override
    public String findUrlByAppAndVersion(VersionEntity appversion) {
        //如果数据库中不存在，则到镜像仓库上找
        VersionEntity v1 = versionDao.get(appversion.getId());
        if(versionDao.get(appversion.getId()).getUrl()==null || versionDao.get(appversion.getId()).getUrl().length()==0) {
            String appName = appDao.findByPrimaryKey(appversion.getAppId()).getApp();
            HarborArtifactServiceImpl ha = new HarborArtifactServiceImpl();
           if(ha.findAppVersionfromHaber(appName,appversion.getVersionName())){
               return "镜像在仓库存在，点击生成地址，请稍后再试";
           }
           return "镜像在仓库不存在,请确认版本";
        };
        //地址存在与数据库中
        return versionDao.get(appversion.getId()).getUrl();
    }

    //<!--用户自定义代码结束-->
}
